﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HQHS.Library.DAO.Menu
{
    public class MenuInfo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Link { get; set; }
        //type=1 website menu, type=2 admin menu
        public byte Type { get; set; }
        public int DisplayOrder { get; set; }
        public int? ParentId { get; set; }
        public string InsertedBy { get; set; }
        public string UpdatedBy { get; set; }
        public DateTime? InsertedDate { get; set; }
        public DateTime? UpdatedDate { get; set; }
    }

    public class MenuInfoAssembler
    {
        private MenuInfoAssembler()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        public static IList<MenuInfo> CreateList(SqlDataReader dr)
        {
            IList<MenuInfo> list = new List<MenuInfo>();

            if (dr != null)
            {
                // Reader ordinals
                int idOrd;
                int nameOrd;
                int linkOrd;
                int typeOrd;
                int displayOrd;
                int parentIdOrd;
                int insertedByOrd;
                int updatedByOrd;
                int insertedDateOrd;
                int updatedDateOrd;

                // Get ordinals
                idOrd = dr.GetOrdinal("ID");
                nameOrd = dr.GetOrdinal("Name");
                linkOrd = dr.GetOrdinal("Link");
                typeOrd = dr.GetOrdinal("Type");
                displayOrd = dr.GetOrdinal("DisplayOrder");
                parentIdOrd = dr.GetOrdinal("ParentId");
                insertedByOrd = dr.GetOrdinal("InsertedBy");
                updatedByOrd = dr.GetOrdinal("UpdatedBy");
                insertedDateOrd = dr.GetOrdinal("InsertedDate");
                updatedDateOrd = dr.GetOrdinal("UpdatedDate");

                // Read objects and add to list
                while (dr.Read())
                {
                    MenuInfo info = new MenuInfo();

                    info.ID = GetValue<int>(dr, idOrd, 0);
                    info.Name = GetValue<string>(dr, nameOrd, null);
                    info.Link = GetValue<string>(dr, linkOrd, null);
                    info.Type = GetValue<byte>(dr, typeOrd, 1);
                    info.DisplayOrder = GetValue<int>(dr, displayOrd, 0);
                    info.ParentId = GetValue<int?>(dr, parentIdOrd, null);
                    info.InsertedBy = GetValue<string>(dr, insertedByOrd, null);
                    info.UpdatedBy = GetValue<string>(dr, updatedByOrd, null);
                    info.InsertedDate = GetValue<DateTime?>(dr, insertedDateOrd, null);
                    info.UpdatedDate = GetValue<DateTime?>(dr, updatedDateOrd, null);

                    list.Add(info);
                }
            }

            return list;
        }

        private static TValue GetValue<TValue>(SqlDataReader dr, int ordinal, TValue dbNullValue)
        {
            TValue value;

            if (dr.IsDBNull(ordinal))
            {
                return dbNullValue;
            }
            else
            {
                value = (TValue)dr.GetValue(ordinal);

                if (value.GetType() == typeof(string))
                {
                    value = (TValue)RTrimString(value);
                }

                return value;
            }

        }

        private static object RTrimString(object value)
        {
            if (value != null)
            {
                return ((string)value).TrimEnd(' ');
            }
            else
            {
                return null;
            }
        }
    }
}
